<template>
	<view id="search">

	<view style="padding:20rpx;background-color: #fff;position: sticky;top:0;z-index: 2;">
		<u-search focus=true @search="getSearchContent(true)" @custom="getSearchContent(true)" font-size="28" :clearabled="true" placeholder="请输入商品" v-model="keyWord"></u-search>
	</view>		
	
	<u-waterfall v-model="flowList" ref="uWaterfall">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @click="navigateToDetail(item.goodsId)">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load threshold="-450" border-radius="10" :image="item.goodsThumbUrl" :index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.goodsName}}
						</view>
						<view class="demo-price">
							{{item.price}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							乌云下旗舰店
						</view>
						<u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close" @click="remove(item.id)"></u-icon>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @click="navigateToDetail(item.goodsId)">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.goodsThumbUrl" :index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.goodsName}}
						</view>
						<view class="demo-price">
							{{item.price}}元
						</view>
						<view class="demo-tag">
							<view class="demo-tag-owner">
								自营
							</view>
							<view class="demo-tag-text">
								放心购
							</view>
						</view>
						<view class="demo-shop">
							乌云下旗舰店
						</view>
						<u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close" @click="remove(item.id)"></u-icon>
					</view>
				</template>
		</u-waterfall>
		
		<view v-show="noData" style="text-align: center;">
			<u-empty text="没有找到商品..." mode="search"></u-empty>
		
		</view>
		<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus"  :load-text="loadText" @loadmore="addRandomData"></u-loadmore>
		
	</view>
</template>

<script>
	import { search } from '../../../util/api/request.js'
	export default {
		data() {
			return {
				// list: [
					
				// ],
				keyWord: "",
				noData: false,
				
				loadStatus: 'loadmore',
				flowList: [],
				list: [],
				page: 1,
				category: 'vip',
				loadText: {
					loadmore: '',
					loading: '努力加载中...',
					nomore: '没有更多了...'
				},
			}
		},
		
		onLoad(options) {
			// this.addRandomData();
			this.category = options.category
		},
		
		onReachBottom() {
			this.loadStatus = 'loading';
			this.page ++;
			// 模拟数据加载
			setTimeout(() => {
				this.getSearchContent();
				this.loadStatus = 'loadmore';
			}, 1000)
		},
		
		methods:{
			
			addRandomData() {
				this.getSearchContent();
				// for(let i = 0; i < 10; i++) {
				// 	let index = this.$u.random(0, this.list.length - 1);
				// 	// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
				// 	let item = JSON.parse(JSON.stringify(this.list[index]))
				// 	item.id = this.$u.guid();
				// 	this.flowList.push(item);
				// }
			},
			remove(id) {
				this.$refs.uWaterfall.remove(id);
			},
			clear() {
				this.$refs.uWaterfall.clear();
			},
						
						
			/**
			 * 搜索内容
			 */
			getSearchContent(flag) {
				let that = this;
				/* pub_id 接口秘钥  Y
				* sid 自定义参数，不能是中文，可以是数字字母组合 Y
				* keyword 搜索关键词 Y
				* sort	 1-综合排序,2-价格升序,3-销量降序 默认1
				* source 来源 jd-京东,vip-唯品会,pdd-拼多多,kaola-考拉,taobao-淘宝 默认vip
				* page 页码 默认 1
				* pageSize 每页显示多少，默认20
				* sub_share_rate  分成比例 1代表100%，0.9代表90%，默认1
				* */
				that.noData = false;
				this.loadStatus = 'loading'
				if(flag) {
					that.page = 1;
					that.list = [];
					that.flowList = [];
					that.clear();
				}
				let obj = {};
				obj.sid = Math.floor(Math.random()*10000000000000);
				obj.keyword = that.keyWord;
				obj.page = that.page || 1;
				obj.source = that.category;
				search(obj).then(res=>{
					console.log(res.list)
					
					that.list = that.list.concat(res.data);
					that.flowList = that.flowList.concat(res.data);
					if(that.flowList.length==0) {
						that.loadStatus = 'loadmore'
						that.noData = true;
					}
					console.log("res.data:", that.flowList)
				})
			},
			
			
			/**
			 * @param {Object} goodsId  商品id
			 * 转到详情
			 */
			navigateToDetail(goodsId) {
		
				uni.navigateTo({
					url: "/pages/index/goodsDetail/goodsDetail?goodsId="+goodsId
				})
			
				
			},
		}
	}
</script>

<style>
	/* page不能写带scope的style标签中，否则无效 */
	page {
		background-color: rgb(240, 240, 240);
	}
</style>


<style lang="scss" scoped>
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}
	
	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}
	
	.demo-image {
		width: 100%;
		border-radius: 4px;
	}
	
	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}
	
	.demo-tag {
		display: flex;
		margin-top: 5px;
	}
	
	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}
	
	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}
	
	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}
	
	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>
</style>
